package com.szqhyxj.youxiangju.biz.partnerHome.my;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.my.MyModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PartnerIconAdapter extends RecyclerView.Adapter<PartnerIconAdapter.IconViewHolder> {

    private List<PartnerMyModel.IconItem> iconItems;

    private IconClickListener iconClickListener;

    public PartnerIconAdapter(List<PartnerMyModel.IconItem> iconItems, IconClickListener iconClickListener) {
        this.iconItems = iconItems;
        this.iconClickListener = iconClickListener;
    }

    @NonNull
    @Override
    public IconViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new IconViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.merchant_func_icon_item_layout, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull IconViewHolder iconViewHolder, int i) {
        final PartnerMyModel.IconItem iconItem = iconItems.get(i);
        iconViewHolder.itemView.setOnClickListener(null);
        iconViewHolder.title.setText(iconItem.getTitle());
        iconViewHolder.icon.setImageResource(iconItem.getIcon());
        iconViewHolder.subTitle.setText(iconItem.getSubTitle());
        if (iconItem.getSubTitleColor() != 0) {
            iconViewHolder.subTitle.setTextColor(iconItem.getSubTitleColor());
        }
        iconViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iconClickListener.onClickItem(iconItem.getId());
            }
        });
    }

    @Override
    public int getItemCount() {
        return iconItems.size();
    }

    static class IconViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.icon)
        ImageView icon;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.sub_title)
        TextView subTitle;

        IconViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface IconClickListener {
        void onClickItem(PartnerMyModel.ICON icon);
    }
}